package org.util.groupUtil;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.site.group.groupBean;
import org.springframework.orm.hibernate3.HibernateTemplate;


public class groupHibernaterUtil {


    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new AnnotationConfiguration().configure().buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
    public static void shutdown() {
    	// Close caches and connection pools
    	getSessionFactory().close();
    }
    
    
	private HibernateTemplate hibernateTemplate;
	
	public void setSessionFactory(SessionFactory sessionFactory)
	{
	this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}
	
	public void saveUser(groupBean group) {
	hibernateTemplate.saveOrUpdate(group);
	}
	
	@SuppressWarnings("unchecked")
	public List<groupBean> listUser() {
	return hibernateTemplate.find("from stock");
	}
}